import { defineStore } from "pinia";
import { parse, stringify } from "zipson";
import { find, isEmpty, remove } from "lodash-es";

export const useAiStore = defineStore(
  "ai",
  () => {
    /* 所有会话
    {
      time:"", //会话唯一标识，是个时间戳，
      title:"新建会话", //会话标题
      status:"end", //start、running、stop、end 用来切换会话时判断当前是否有正在输出的对话。stop手动停止，end程序自己结束
      lastQuestion:"", //最后提问的内容
      flag: true // 标记当前问答是否已经回答了
    }
     */
    let dialogues = ref({});

    // 当前会话
    let currDialogue = ref({});

    /* 所有会话的消息记录。格式为 
      time:{
          dialogues:[], // 对话记录
          vectorData:[], //用来保存当前会话的向量数据，用来判断当前会话是否需要重新生成向量。
      }
    */
    let dialogueRecords = ref({});

    // 缓存会话
    function cacheDialogue({ time, title, status, flag }) {
      dialogues.value[time] = { time, title, status, flag };
      initDialogueRecords(time);
    }
    // 初始化会话记录
    function initDialogueRecords(time) {
      dialogueRecords.value[time] = {
        dialogues: [],
        vectorData: []
      };
    }
    // 删除会话和消息
    function delDialogue(time) {
      delete dialogues.value[time];
    }
    // 修改历史会话和当前会话。title标题 、status状态、flag首页跳转过来的问题是否已经处理的标记
    function mergeDialogue({ title, status, flag }) {
      // 标题初始化是“新建对话”，防止重复修改标题
      if (title && dialogueRecords.value[currDialogue.value.time].title == "新建对话") {
        dialogues.value[currDialogue.value.time].title = title;
        currDialogue.value.title = title;
      }
      if (status) {
        dialogues.value[currDialogue.value.time].status = status;
        currDialogue.value.status = status;
      }
      if (flag) {
        dialogues.value[currDialogue.value.time].flag = flag;
        currDialogue.value.flag = flag;
      }
    }

    // 设置当前会话
    function setCurrDialogue(dialogue) {
      currDialogue.value = dialogue;
    }

    // 添加右侧会话记录，并记录最后的问题
    function addRightDialogueRecord({ dialogue, content }) {
      dialogueRecords.value[currDialogue.value.time].dialogues.push(dialogue);
      dialogues.value[currDialogue.value.time].lastQuestion = content;
      currDialogue.value.lastQuestion = content;
    }

    /* 添加左侧会话记录
    time：会话唯一标识
    dialogue：对话内容
    */
    function addLeftDialogueRecord(dialogue) {
      dialogueRecords.value[currDialogue.value.time].dialogues.push(dialogue);
    }

    /* 修改左侧会话记录
    time：会话唯一标识
    dialogue：对话内容
    tag：消息的唯一标识
    */
    function editLeftDialogueRecord({ dialogue, tag, vectorData }) {
      // 修改对话记录
      if (dialogue) {
        dialogueRecords.value[currDialogue.value.time].dialogues.forEach((item) => {
          if (item.tag === tag) {
            item = dialogue;
            return;
          }
        });
      }
      // 修改向量数据
      if (vectorData?.length > 0) {
        dialogueRecords.value[currDialogue.value.time].vectorData = vectorData;
      }
    }

    // 删除左侧会话记录
    function delLeftDialogueRecord(tag) {
      remove(dialogueRecords.value[currDialogue.value.time].dialogues, ["tag", tag]);
    }

    return {
      dialogues,
      currDialogue,
      dialogueRecords,
      cacheDialogue,
      delDialogue,
      mergeDialogue,
      setCurrDialogue,
      addRightDialogueRecord,
      addLeftDialogueRecord,
      editLeftDialogueRecord,
      delLeftDialogueRecord
    };
  },
  {
    persist: {
      paths: ["dialogues", "currDialogue", "dialogueRecords"],
      serializer: {
        deserialize: parse,
        serialize: stringify
      }
    }
  }
);
